package com.example.ymm.modules1.service.impl.hl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.modules1.entity.hl.dkn.DknForecastSave;
import com.example.ymm.modules1.mapper.hl.DknForecastSaveMapper;
import com.example.ymm.modules1.service.hl.DknForecastSaveService;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
@DSTransactional
@Slf4j
@DS("slave2")
public class DknForecastSaveServiceImpl extends BaseServiceImpl<DknForecastSaveMapper, DknForecastSave> implements DknForecastSaveService {

    @Override
    public void saveForecast(JSONArray list) {
        for (int i = 0; i < list.size(); i++) {
            JSONObject p_json = list.getJSONObject(i);
            String p_modelCode = p_json.getString("modelCode");

            JSONArray children = p_json.getJSONArray("children");
            for (int j = 0; j < children.size(); j++) {
                JSONObject child = children.getJSONObject(j);
                String modelCode = child.getString("modelCode");
                /**
                 * QTY Shipped Out
                 * Updated Un-Finished Planned DO
                 * New Planned DO (EPD)
                 *
                 * "QTY Shipped Out".equals(modelCode)||
                 */
                if("Updated Un-Finished Planned DO".equals(modelCode)
                        ||"New Planned DO (EPD)".equals(modelCode)){
                    Set<String> keys = child.keySet();
                    JSONObject total=new JSONObject();
                    total.put("modelCode",modelCode);
                    boolean emply=true;
                    for (String key : keys) {
                        if(key.contains("-W")&& UtilValidate.isNotEmpty(child.get(key))&&child.getDouble(key)!=0){
                            total.put(key,child.get(key));
                            emply=false;
                        }
                    }
                    if(!emply){
                        DknForecastSave forecastSave = getModelCodeAndItemName(p_modelCode, modelCode);
                        if(UtilValidate.isNotEmpty(forecastSave)){
//                            forecastSave.setUserId(getUserId());
//                            forecastSave.setUserName(getUserName());
                            forecastSave.setTxtJson(total.toJSONString());
                            saveOrUpdate(forecastSave);
                        }else{
                            forecastSave =new DknForecastSave();
                            forecastSave.setItemName(modelCode);
                            forecastSave.setUserId(getUserId());
                            forecastSave.setUserName(getUserName());
                            forecastSave.setTxtJson(total.toJSONString());
                            forecastSave.setModelCode(p_modelCode);
                            saveOrUpdate(forecastSave);
                        }
                    }

                }
            }
        }
    }

    @Override
    public void cleanForecast() {
        remove(null);
    }

    @Override
    public DknForecastSave getModelCodeAndItemName(String modelCode, String itemName) {
        LambdaQueryWrapper<DknForecastSave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DknForecastSave::getModelCode, modelCode);
        queryWrapper.eq(DknForecastSave::getItemName, itemName);
        List<DknForecastSave> dknForecastSaves = baseMapper.selectList(queryWrapper);
        if(dknForecastSaves.size()>0){
            return dknForecastSaves.get(0);
        }
        return null;
    }

    @Override
    public List<DknForecastSave> getInModelCode(List<String> modelCodeIn) {
        LambdaQueryWrapper<DknForecastSave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DknForecastSave::getModelCode, modelCodeIn);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<DknForecastSave> findListByModelCode(String modelCode, List<DknForecastSave> list) {
        List<DknForecastSave> result = new ArrayList<>();
        for (DknForecastSave dknForecastSave : list) {
            if(dknForecastSave.getModelCode().equals(modelCode)){
                result.add(dknForecastSave);
            }
        }
        return result;
    }

    @Override
    public DknForecastSave findModelCodeAndItemName(String modelCode, String itemName, List<DknForecastSave> list) {
        DknForecastSave result = null;
        for (DknForecastSave dknForecastSave : list) {
            if(dknForecastSave.getModelCode().equals(modelCode)&&dknForecastSave.getItemName().equals(itemName)){
                result=dknForecastSave;
            }
        }
        return result;
    }
}
